/**
 * Ingenius-Group S.A.S
 *
 * Project: SAU
 * File: EditIndicatorView.java
 * Date: 10/03/2011
 */

package com.ingenius.sau.client.view;

import java.util.List;

import com.extjs.gxt.ui.client.widget.form.Radio;
import com.extjs.gxt.ui.client.widget.form.RadioGroup;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.FormData;
import com.ingenius.sau.client.presenter.EditUserPresenter.EditUserDisplay;
import com.ingenius.sau.client.view.widgets.ListBox;
import com.ingenius.sau.shared.model.users.Role;

/**
 * Class <strong>EditIndicatorView</strong>
 * 
 * @author Jesus David
 * @version 0.1
 */
public class EditUserView extends AbstractEditView implements EditUserDisplay {

	// UI Widgets
	private final TextField<String> textUsername;
	private final TextField<String> textPassword;
	private final TextField<String> textConfirm;
	private final TextField<String> textFirstname;
	private final TextField<String> textLastname;
	private final ListBox comboRoles;

	// Constructor

	public EditUserView() {
		super();
		textUsername = new TextField<String>();
		textPassword = new TextField<String>();
		textPassword.setPassword(true);
		textConfirm = new TextField<String>();
		textConfirm.setPassword(true);
		textFirstname = new TextField<String>();
		textLastname = new TextField<String>();
		comboRoles = new ListBox();

		// TODO i18n
		setHeading("Edit User");
		setSize(500, 400);
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ingenius.sau.client.view.AbstractEditView#createComponents()
	 */
	@Override
	protected void createComponents() {
		// TODO i18n
		textUsername.setFieldLabel("Username");
		textUsername.setAllowBlank(false);
		textUsername.addPlugin(plugin);
		textUsername.setData("text", "Username may be unique");
		textUsername.getMessages().setBlankText("Username is required");

		textPassword.setFieldLabel("Password");
		textPassword.setAllowBlank(false);
		textPassword.addPlugin(plugin);
		textPassword.setData("text", "Enter the password");
		textPassword.getMessages().setBlankText("Password is required");

		textConfirm.setFieldLabel("Confirm");
		textConfirm.setAllowBlank(false);
		textConfirm.addPlugin(plugin);
		textConfirm.setData("text", "Please confirm the password");
		textConfirm.getMessages().setBlankText("Password confirm is required");

		textFirstname.setFieldLabel("First name");
		textFirstname.setAllowBlank(false);
		textFirstname.addPlugin(plugin);
		textFirstname.setData("text", "Enter the user first name");
		textFirstname.getMessages().setBlankText("First name is required");

		textLastname.setFieldLabel("Last name");
		textLastname.setAllowBlank(false);
		textLastname.addPlugin(plugin);
		textLastname.setData("text", "Please confirm the password");
		textLastname.getMessages().setBlankText("Last name is required");

		comboRoles.setFieldLabel("Role");
		comboRoles.setAllowBlank(false);
		comboRoles.setEditable(false);
		comboRoles.addPlugin(plugin);
		comboRoles.setData("text", "Select the user role");
		comboRoles.getMessages().setBlankText("Role is required");

		Radio radio = new Radio();
		radio.setBoxLabel("Yes");
		radio.setValue(true);

		Radio radio2 = new Radio();
		radio2.setBoxLabel("No");

		RadioGroup radioGroup = new RadioGroup();
		radioGroup.setFieldLabel("Pwd change required");
		radioGroup.add(radio);
		radioGroup.add(radio2);

		FormData formData = new FormData("-20");
		form.add(textUsername, formData);
		form.add(textPassword, formData);
		form.add(textConfirm, formData);
		form.add(textFirstname, formData);
		form.add(textLastname, formData);
		form.add(comboRoles, formData);
		form.add(radioGroup, formData);
	}

	@Override
	protected boolean isValid() {
		if (textPassword.getValue().equals(textConfirm.getValue())) {
			return super.isValid();
		} else {
			showInfo("Password is not the same...");
			return false;
		}
	}

	@Override
	public String getUsername() {
		return textUsername.getValue();
	}

	@Override
	public void setUsername(String username) {
		textUsername.setValue(username);
	}

	@Override
	public String getPassword() {
		return textPassword.getValue();
	}

	@Override
	public void setPassword(String password) {
		textPassword.setValue(password);
	}

	@Override
	public String getPasswordConfirm() {
		return textConfirm.getValue();
	}

	@Override
	public void setPasswordConfirm(String confirm) {
		textConfirm.setValue(confirm);
	}

	@Override
	public void setFirstname(String firstname) {
		textFirstname.setValue(firstname);
	}

	@Override
	public String getFirstname() {
		return textFirstname.getValue();
	}

	@Override
	public void setLastname(String lastname) {
		textLastname.setValue(lastname);
	}

	@Override
	public String getLastname() {
		return textLastname.getValue();
	}

	@Override
	public String getSelectedRoleId() {
		return comboRoles.getSimpleValue();
	}

	@Override
	public void setSelectedRoleId(String roleId) {
		comboRoles.setSimpleValue(roleId);
	}

	@Override
	public void setRoles(List<Role> roles) {
		for (Role role : roles) {
			comboRoles.add(role.getId(), role.getName());
		}
	}
}
